package dat255.calendarclock.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * 
 * @author Gabriel Andersson, Marcus Hultman
 *
 */
public class FilterBar extends LinearLayout implements PropertyChangeListener{
	
	// Fields
	public static final int WIDTH_EXPANDED = 75;
	public static final int WIDTH_HIDDEN = 15;
	
	private static boolean isInitialized = false;
	private static boolean isExpanded = false;
	
	private static PropertyChangeSupport syncHost;
	
	private static Paint mainPaint = new Paint();
	private static Paint secondaryPaint = new Paint();
	private static Paint thirdPaint = new Paint();
	private static Paint dotPaint = new Paint();
	
	private GestureDetector gestureDetector;
	private static GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {

		@Override
		public boolean onDown(MotionEvent e) {
			return true;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			syncHost.firePropertyChange("isExpanded", isExpanded, !isExpanded);
			return false;
		}
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if ((velocityX > 0 && !isExpanded) || (velocityX < 0 && isExpanded)){
				syncHost.firePropertyChange("isExpanded", isExpanded, !isExpanded);
			}
			return false;
		}
		
	};
	
	/**
	 * Constructor.
	 * @param context
	 * @param attrs
	 */
	public FilterBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		setOrientation(VERTICAL);

		gestureDetector = new GestureDetector(this.getContext(),
				gestureListener);
		
		isExpanded = !!false; //:D
		
		if (!isInitialized)
			init();
	
		syncHost.addPropertyChangeListener(this);
	}

	/**
	 * Initializes all static members once.
	 */
	private void init(){
		mainPaint.setColor(Color.DKGRAY);
		mainPaint.setStyle(Paint.Style.FILL);
		
		secondaryPaint.setColor(Color.WHITE);
		secondaryPaint.setStyle(Paint.Style.FILL);
		
		thirdPaint.setColor(Color.LTGRAY);
		thirdPaint.setStyle(Paint.Style.FILL);
		
		dotPaint.setColor(Color.BLACK);
		dotPaint.setStyle(Paint.Style.FILL);
		
		syncHost = new PropertyChangeSupport(FilterBar.class);
		
		isInitialized = true;
	}

	// Methods
	
	private void setExpanded(boolean isExpanded){
		FilterBar.isExpanded = isExpanded;
		if (isExpanded) {
			setWidth(WIDTH_EXPANDED);
			for (int i = 0; i < getChildCount(); i++) {
				((FilterButton) getChildAt(i)).setVisible(true);
			}
		} else {
			setWidth(WIDTH_HIDDEN);
			for (int i = 0; i < getChildCount(); i++) {
				((FilterButton) getChildAt(i)).setVisible(false);
			}
		}	
	}
	
	private void setWidth(int width){
		this.setLayoutParams(new LayoutParams(width, getHeight()));
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	
	/**
	 * Called by the firePropertyChange
	 */
	public void propertyChange(PropertyChangeEvent e) {
		setExpanded((Boolean)e.getNewValue());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawRect(0, 0, getWidth(), getHeight(), mainPaint);
		canvas.drawRect(getWidth() - WIDTH_HIDDEN, 0, getWidth() - (WIDTH_HIDDEN / 2f), getHeight(), secondaryPaint);
		canvas.drawRect(getWidth() - (WIDTH_HIDDEN / 2f), 0, getWidth(), getHeight(), thirdPaint);
		for (int i = 0; i < 3; i++)
			canvas.drawCircle(getWidth() - (WIDTH_HIDDEN / 2f), (i + 1) * 10, 3, dotPaint);
	}

}
